package com.huawei123.hasee.cniao5store.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.huawei123.hasee.cniao5store.view.LoadingPager;

/**
 * 项目名称：Cniao5Store
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 下午 4:39
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 下午 4:39
 * 修改备注：
 */

public abstract class BaseFragment extends Fragment implements BaseToast {
    private LoadingPager loadingPager;
    protected BaseActivity myActivity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myActivity = (BaseActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (loadingPager == null) {
            /*控件调用和加载数据回传的方法*/
            loadingPager = new LoadingPager(getContext()) {
                @Override
                protected void loading() {
                    BaseFragment.this.loading();
                }

                @Override
                protected View createSuccessView() {
                    return BaseFragment.this.createSuccessView();
                }
            };
        }
        return loadingPager;
    }

    /**
     * 调loadingPager的show（）方法
     */
    public void show() {
        if (loadingPager != null) {
            loadingPager.show();
        }
    }

    /**
     * 调loadingPager的setState（）方法
     *
     * @param state
     */
    protected void setState(LoadingPager.LoadResult state) {
        if (loadingPager != null) {
            loadingPager.setState(state);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void showToast(String message) {
        myActivity.showToast(message);
    }

    protected abstract void loading();

//    public abstract void initView();

    protected abstract View createSuccessView();

}
